<template>
  <div class="login-container">
    <div class="login-box">
      <h2>银行登录系统</h2>
      <div class="input-group">
        <label>卡号：</label>
        <input type="text" v-model="cardno" placeholder="请输入银行卡号" @keyup.enter="handleLogin">
      </div>
      <div class="input-group">
        <label>密码：</label>
        <input type="password" v-model="password" placeholder="请输入密码" @keyup.enter="handleLogin">
      </div>
      <button @click="handleLogin" :disabled="loading">
        {{ loading ? '登录中...' : '登录' }}
      </button>
    </div>
  </div>
</template>

<script>
import md5 from 'md5'

export default {
  name: 'Login',
  data() {
    return {
      cardno: '',
      password: '',
      loading: false
    }
  },
  created() {
    // this.$store.commit('clearUserInfo');
  },
  methods: {
    // 登录按钮的点击事件处理函数
    async handleLogin() {
      if (!this.cardno || !this.password) {
        this.$message.warning('请输入卡号和密码');
        return;
      }
      this.loading = true;
      try {
        const res = await this.$axios({
          method: 'post',
          url: '/bank/login',
          data: {
            cardno: this.cardno,
            password: md5(this.password)
          }
        });

        if (res.data.code === 0) {
          this.$store.commit('setUserInfo', {
            token: res.data.data.token,
            userInfo: res.data.data.userInfo
          });

          this.$axios.defaults.headers.common['Authorization'] = `Bearer ${res.data.data.token}`;

          this.$message.success('登录成功');
          await this.$router.replace('/dashboard');
        } else {
          this.$message.error(res.data.msg || '登录失败')
        }
      } catch (error) {
        console.error('登录失败:', error);
        this.$message.error('登录失败，请重试')
      } finally {
        this.loading = false;
      }
    }
  }
}
</script>

<style scoped>
/* 添加在每个组件的 style 标签内的开头 */
* {
    user-select: none;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
}

input, 
button, 
select, 
textarea {
    user-select: text;
    -webkit-user-select: text;
    -moz-user-select: text;
    -ms-user-select: text;
    cursor: pointer;
}

input, 
textarea {
    cursor: text;
}
.login-container {
    min-height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    background: linear-gradient(135deg, #f5f7fa 0%, #e4e7eb 100%);
    padding: 20px;
}

.login-box {
    width: 400px;
    background: white;
    border-radius: 20px;
    padding: 40px;
    box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1);
}

h2 {
    color: #2c3e50;
    font-size: 28px;
    text-align: center;
    margin-bottom: 30px;
}

.input-group {
    margin-bottom: 25px;
}

label {
    display: block;
    color: #2c3e50;
    font-size: 16px;
    margin-bottom: 10px;
}

input {
    width: 100%;
    padding: 12px 15px;
    border: 2px solid #eee;
    border-radius: 10px;
    font-size: 16px;
    transition: all 0.3s ease;
}

input:focus {
    border-color: #409EFF;
    box-shadow: 0 0 0 3px rgba(64, 158, 255, 0.1);
}

button {
    width: 100%;
    padding: 15px;
    background: linear-gradient(135deg, #409EFF 0%, #66b1ff 100%);
    color: white;
    border: none;
    border-radius: 10px;
    font-size: 16px;
    cursor: pointer;
    transition: all 0.3s ease;
}

button:hover:not(:disabled) {
    transform: translateY(-2px);
    box-shadow: 0 5px 15px rgba(64, 158, 255, 0.3);
}

button:disabled {
    background: #cccccc;
    cursor: not-allowed;
}
</style>
